import 'package:flutter/material.dart';

import 'home.dart';
import 'category.dart';
import 'message.dart';
import 'mine.dart';

class Index extends StatefulWidget {
  const Index({Key? key}) : super(key: key);

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<Index> {
  int _index = 0;
  var appTitle = "a app";

  List<Widget> _homeWidgets = [Home(), Category(), Message(), Mine()];

  void _onButtonNavigationBarTapped(index) {
    setState(() {
      _index = index;
    });
  }

  BottomNavigationBarItem _getBottomNavItem(
      String title, String normalIcon, String activeIcon, int index) {
    return BottomNavigationBarItem(
        icon: _index == index
            ? Image.asset(activeIcon, width: 32, height: 32)
            : Image.asset(normalIcon,width: 32,height: 32,),
        label: title,
        );
        
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(primarySwatch: Colors.green),
        home: Scaffold(
          appBar: AppBar(
            title: Text(appTitle),
          ),
          body: IndexedStack(
            index: _index,
            children: _homeWidgets,
          ),
          bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              currentIndex: _index,
              onTap: _onButtonNavigationBarTapped,
              selectedItemColor:Colors.red,//自定义主题色，否则继承theme属性的配置
              items: [
                _getBottomNavItem('首页', 'images/navbar/home_icon.png',
                    'images/navbar/home_icon_a.png', 0),
                _getBottomNavItem('分类', 'images/navbar/cate_icon.png',
                    'images/navbar/cate_icon_a.png', 1),
                _getBottomNavItem('消息', 'images/navbar/msg_icon.png',
                    'images/navbar/msg_icon_a.png', 2),
                _getBottomNavItem('我的', 'images/navbar/mine_icon.png',
                    'images/navbar/mine_icon_a.png', 3),
              ]),
        ));
  }
}
